package com.synjones.visitor;

import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.hardware.display.DisplayManager;
import android.net.Uri;
import android.os.Build;
import android.provider.Settings;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Display;
import android.view.TextureView;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import com.blankj.utilcode.util.DeviceUtils;
import com.blankj.utilcode.util.PermissionUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.synjones.face.camera.FaceHelper;
import com.synjones.face.camera.FaceRectView;
import com.synjones.idcard.IDCard;
import com.synjones.idcardusb_reader.R;

public class CardInfoActivity extends AppCompatActivity implements CardInfoContract.View {

    private CardInfoContract.Presenter presenter;
    private ImageView face;
    private TextView idNo;
    private TextView name;
    private TextureView faceCamera;
    private FaceRectView cameraRect;
    private FaceHelper faceHelper;
    private TextView sex;
    private TextView nation;
    private TextView time;
    private TextView address;
    private TextView office;
    private TextView tip;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_card_info);
        initView();
//        checkPermission();
        presenter = new CardInfoPresenter(this,faceHelper);
        presenter.start();
    }

    private void initView(){
        face = (ImageView) findViewById(R.id.face);
        idNo = (TextView) findViewById(R.id.idcardno);
        name = (TextView) findViewById(R.id.idcardname);
        sex = (TextView) findViewById(R.id.sex);
        nation = (TextView) findViewById(R.id.nation);
        address = (TextView) findViewById(R.id.address);
        time = (TextView) findViewById(R.id.time);
        office = (TextView) findViewById(R.id.office);
        tip = (TextView) findViewById(R.id.tip);
        faceCamera = (TextureView) findViewById(R.id.cameraTexture);
        cameraRect = (FaceRectView) findViewById(R.id.cameraRect);
        faceHelper = new FaceHelper(faceCamera, cameraRect, this,this);
    }

    @Override
    public void showIdCardInfo(final IDCard idCard) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                face.setImageBitmap(idCard.getPhoto());
                idNo.setText(idCard.getIDCardNo());
                name.setText(idCard.getName());
                sex.setText(idCard.getSex());
                nation.setText(idCard.getNation());
                time.setText(idCard.getUserLifeBegin()+"-"+idCard.getUserLifeEnd());
                address.setText(idCard.getAddress());
                office.setText(idCard.getGrantDept());
            }
        });
        face.postDelayed(new Runnable() {
            @Override
            public void run() {
                face.setImageBitmap(null);
                idNo.setText("");
                name.setText("");
                sex.setText("");
                nation.setText("");
                office.setText("");
                address.setText("");
                time.setText("");
            }
        }, 3000);
    }

    @Override
    public void showCompareInfo(final float info) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                Double d = new Double(info);
                tip.setTextColor(Color.RED);
                if (info<0.01) {
                    tip.setText("对比得分：0, 不是同一人");
                }else if (info < 0.6){
                    tip.setText("对比得分："+ d.toString().substring(0,5) +", 不是同一人");
                }else if (info < 0.8){
                    tip.setTextColor(Color.YELLOW);
                    tip.setText("对比得分："+ d.toString().substring(0,5) +", 可能是同一人");
                }else {
                    tip.setTextColor(Color.GREEN);
                    tip.setText("对比得分："+ d.toString().substring(0,5) +", 基本是同一人");
                }
            }
        });

        tip.postDelayed(new Runnable() {
            @Override
            public void run() {
                tip.setText("");
            }
        },3000);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        presenter.release();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode){
            case 101:
                if(Build.VERSION.SDK_INT >22){
                    if (!Settings.canDrawOverlays(this)){
                        ToastUtils.showShort("授权悬浮窗！！！！");
                        Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
                                Uri.parse("package:$packageName"));
                        startActivityForResult(intent, 101);
                    }else{
//                        secondScreen?.show()
                    }
                }
                break;
                default: super.onActivityResult(requestCode, resultCode, data);
        }

    }


    /**
     * 获取授权
     */
    private void checkPermission() {
        if (DeviceUtils.getSDKVersionCode() >= Build.VERSION_CODES.M) {
            PermissionUtils
                    .permission(Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE,Manifest.permission.READ_PHONE_STATE)
                    .callback(new PermissionUtils.SimpleCallback() {
                                  @Override
                                  public void onGranted() {
                                      faceHelper.initFaceEngine();
                                  }

                                  @Override
                                  public void onDenied() {
                                      ToastUtils.showShort("必须授予相机和读写权限！！！");
                                      Intent intent = new Intent();
                                      intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
                                      intent.setData(Uri.fromParts("package", getPackageName(), null));
                                      startActivity(intent);
                                      System.exit(0);
                                  }
                              }
                    )
                    .request();
        } else {
            faceHelper.initFaceEngine();
        }
    }
}
